<?php

declare(strict_types=1);
/**
 * This file is part of MineAdmin.
 *
 * @link     https://www.mineadmin.com
 * @document https://doc.mineadmin.com
 * @contact  root@imoi.cn
 * @license  https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
 */

namespace App\Schema\Word;

use App\Model\Word;
use App\Model\Permission\User;
use App\Model\WordGroup;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;

#[Schema]
final class WordSchema implements \JsonSerializable
{
    public ?string $remark;

    public function __construct(
        Word $model,
        #[Property(property: 'word', title: '单词或短语', type: 'string')]
        public ?string $word,
        #[Property(property: 'interpretation', title: '中文释义', type: 'string')]
        public ?string $interpretation,
        #[Property(property: 'origin_word', title: '原词本名称', type: 'string')]
        public ?string $origin_word,
        #[Property(property: 'phonetic_alphabet', title: '音标', type: 'string')]
        public ?string $phonetic_alphabet,
        #[Property(property: 'group_id', title: '分组ID', type: 'int')]
        public ?int $group_id,
        #[Property(property: 'group', title: '分组', type: 'array')]
        public ?WordGroup $group,
        #[Property(property: 'creator_id', title: '创建者', type: 'int')]
        public ?int $creator_id,
        #[Property(property: 'creator', title: '创建者', type: 'array')]
        public ?User $creator
    ) {

        foreach (
            get_class_vars(__CLASS__)
            as $var
        ) {
            $this->{$var} = $model->${$var};
        }
    }

    public function jsonSerialize(): mixed
    {
        $json = [];
        foreach (
            get_class_vars(__CLASS__)
            as $var
        ) {
            $json[$var] = $this->{$var};
        }
        return $json;
    }
}
